package com.atguigu.xuexiplus.service.impl;

import com.atguigu.xuexiplus.entity.Sitting;
import com.atguigu.xuexiplus.mapper.SittingMapper;
import com.atguigu.xuexiplus.service.SittingService;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 坐姿检测Service实现
 */
@DS("cloud")
@Service
public class SittingServiceImpl implements SittingService {
    
    @Autowired
    private SittingMapper sittingMapper;
    
    @DS("cloud")
    @Override
    public List<Sitting> listAll() {
        return sittingMapper.listAll();
    }
    
    @DS("cloud")
    @Override
    public Map<String, Object> listByPage(int page, int size) {
        // 计算偏移量
        int offset = (page - 1) * size;
        
        // 查询数据
        List<Sitting> data = sittingMapper.listByPage(offset, size);
        
        // 查询总数
        Long total = sittingMapper.countAll();
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("data", data);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        result.put("pages", (long) Math.ceil((double) total / size));
        
        return result;
    }
    
    @DS("cloud")
    @Override
    public List<Sitting> listByTimeRange(String startTime, String endTime) {
        return sittingMapper.listByTimeRange(startTime, endTime);
    }
    
    @DS("cloud")
    @Override
    public Long countAll() {
        return sittingMapper.countAll();
    }
}

